package ar.edu.unq.wannaplay.dtos;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import ar.edu.unq.wannaplay.calendareable.Licence;
import ar.edu.unq.wannaplay.employee.Employee;
import ar.edu.unq.wannaplay.employee.PersonalData;
import ar.edu.unq.wannaplay.employee.Register;
import ar.edu.unq.wannaplay.employee.Skill;
import ar.edu.unq.wannaplay.employee.SocialPlan;
import ar.edu.unq.wannaplay.ocupation.Ocupation;
import ar.edu.unq.wannaplay.utils.DTOHelper;

public class EmployeeDTO extends DTO<Employee> {

    private static final long serialVersionUID = 1L;

    private int id;

    private PersonalData data;

    private RegisterDTO register;

    private List<LicenceDTO> licences;

    private SocialPlan socialPlan;

    private String art;

    private Ocupation ocupation;

    private List<SkillDTO> skills;

    public EmployeeDTO(final Employee entity) {
        super();
        id = entity.getId();
        data = entity.getData();
        register = new RegisterDTO(entity.getRegister());
        socialPlan = entity.getSocialPlan();
        art = entity.getArt();
        ocupation = entity.getOcupation();
        skills = this.generateSkillsDTOs(entity.getSkills());
    }

    public EmployeeDTO() {
        super();
        data = new PersonalData();
    }

    public EmployeeDTO(final PersonalData data, final List<Skill> skills, final List<Licence> licences) {
        super();
        this.data = data;
        this.skills = this.generateSkillsDTOs(skills);
        this.licences = this.generateLicencesDTOs(licences);
    }

    private List<LicenceDTO> generateLicencesDTOs(final List<Licence> theLicences) {
        List<LicenceDTO> licencesDtos = new ArrayList<LicenceDTO>();
        for (Licence lic : theLicences) {
            licencesDtos.add(this.createLicenceDTO(lic));
        }
        return licencesDtos;
    }

    private LicenceDTO createLicenceDTO(final Licence lic) {
        return new LicenceDTO(lic);
    }

    private List<SkillDTO> generateSkillsDTOs(final List<Skill> skillsReal) {
        List<SkillDTO> skillsDtos = new ArrayList<SkillDTO>();
        for (Skill currentSkill : skillsReal) {
            skillsDtos.add(this.createSkillDTO(currentSkill));
        }
        return skillsDtos;
    }

    private SkillDTO createSkillDTO(final Skill currentSkill) {
        return new SkillDTO(currentSkill);
    }

    public void setName(final String name) {
        this.getData().setName(name);
    }

    public void setSurname(final String surname) {
        this.getData().setSurname(surname);
    }

    public void setAddress(final String adress) {
        this.getData().setAddress(adress);
    }

    public void setTelephone(final String telephone) {
        this.getData().setTelephone(telephone);
    }

    public void setEmail(final String email) {
        this.getData().setEmail(email);
    }

    public void setTown(final String town) {
        this.getData().setTown(town);
    }

    public void setUniversity(final String university) {
        this.getData().setUniveristy(university);
    }

    public String getName() {
        return this.getData().getName();
    }

    public String getSurname() {
        return this.getData().getSurname();
    }

    public String getAddress() {
        return this.getData().getAddress();
    }

    public String getTelephone() {
        return this.getData().getTelephone();
    }

    public String getEmail() {
        return this.getData().getEmail();
    }

    public String getTown() {
        return this.getData().getTown();
    }

    public String getUniversity() {
        return this.getData().getUniveristy();
    }

    private PersonalData getData() {
        return data;
    }

    public RegisterDTO getRegister() {
        return register;
    }

    public void setRegister(final RegisterDTO register) {
        this.register = register;
    }

    public SocialPlan getSocialPlan() {
        return socialPlan;
    }

    public void setSocialPlan(final SocialPlan socialPlan) {
        this.socialPlan = socialPlan;
    }

    public String getArt() {
        return art;
    }

    public void setArt(final String art) {
        this.art = art;
    }

    public Ocupation getOcupation() {
        return ocupation;
    }

    public void setOcupation(final Ocupation ocupation) {
        this.ocupation = ocupation;
    }

    public List<SkillDTO> getSkills() {
        return skills;
    }

    public void setSkills(final List<SkillDTO> skills) {
        this.skills = skills;
    }

    // @Override
    // public Employee fromDto() {
    // DTOHelper<Skill, SkillDTO> helper = new DTOHelper<Skill, SkillDTO>();
    // Employee employee = new Employee(this.getRegister().fromDto(),
    // this.getData(), this.getSocialPlan(),
    // this.getArt(), this.getOcupation(), helper.fromDtos(new
    // LinkedList<DTO<Skill>>(this.getSkills())));
    // employee.setId(this.getId());
    // return employee;
    //
    // }

    @Override
    public Employee fromDto() {
        DTOHelper<Skill, SkillDTO> helper = new DTOHelper<Skill, SkillDTO>();
        Employee employee = new Employee(new Register(), this.getData(), this.getSocialPlan(), this.getArt(),
                this.getOcupation(), helper.fromDtos(new LinkedList<DTO<Skill>>(this.getSkills())));
        employee.setId(this.getId());
        return employee;

    }

    public int getId() {
        return id;
    }

    public void setData(final PersonalData data) {
        this.data = data;
    }

    public void setDni(final String dni) {
        this.getData().setId(dni);
    }

    public String getDni() {
        return this.getData().getId();
    }

    public void setId(final int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return data.getName() + " " + data.getSurname() + " " + data.getId();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (art == null ? 0 : art.hashCode());
        result = prime * result + (data == null ? 0 : data.hashCode());
        result = prime * result + id;
        result = prime * result + (ocupation == null ? 0 : ocupation.hashCode());
        result = prime * result + (register == null ? 0 : register.hashCode());
        result = prime * result + (skills == null ? 0 : skills.hashCode());
        result = prime * result + (socialPlan == null ? 0 : socialPlan.hashCode());
        return result;
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        return ((EmployeeDTO) obj).getDni().equals(this.getDni());
    }

    public List<LicenceDTO> getLicences() {
        return licences;
    }

    public void setLicences(final List<LicenceDTO> licence) {
        licences = licence;
    }
}
